<?php
namespace Kxdrmobile\Controller;
use Think\Controller;
class ExtendController extends Controller {
	public $user;
	public function  __construct (){
		parent::__construct();
		Vendor("taobaosdk.top.TopClient");
		Vendor('taobaosdk.lotusphp_runtime.Logger.Logger');
		Vendor("taobaosdk.top.RequestCheckUtil");
		$sid = I("sid");
		if($sid){
			cookie("sid",$sid);
			$this->sid = $sid;
			//cookie保存sid，读取不到则跳出
		}else{
			$this->sid = cookie("sid");
		}
		if(!$this->sid){
			// 没有店铺信息不能访问
			$this->assign("msg","抱歉，本活动只能在店铺内访问");
			$this->display("Index:not_allow");
			die();
		}
		
		$this->mixednick = cookie("tida_mixnick");
		// $this->mixednick = "kekydream";
		// print_r( $_COOKIE);die();
		if(!$this->mixednick){
			// 没有用户信息禁止访问
			
			$this->assign("login",true);
			$this->assign("msg","抱歉，没有读取到您的用户信息，可能您没有登录，请登陆后重试");
			$this->display("Index:not_allow");
			die();
		}
		// $this->user = S($this->mixednick);
		if(!$this->user){
			$USR = D("User");
			$USR->mixednick = $this->mixednick;
			$USR->sid = $this->sid;
			$this->uid = $USR->getUser();
			$this->user = $USR->user_info;
			$this->sys_uid = $USR->user_info["sm_uid"];
			$this->shop_uid = $USR->user_info["shop_uid"];
			S($this->mixednick,$this->user);
		}
		if(!$this->uid){
			$this->uid = $this->user["id"];
		}
		$friend = I("friend");
		// $friend = "test_friend"; //测试邀请人
		
		$FIR = D("Friendships");
		$FIR->sid = $this->sid;
		$FIR->home_id = $this->uid;
		$FIR->home_mixednick = $this->mixednick;
		$codeimg = ($FIR->get_code_img());
// print_r($codeimg);
		if($friend){
			$FIR->home_id=null;
			$FIR->guest_id = $this->uid;
			$FIR->home_mixednick = $friend;
			$FIR->guest_mixednick = $this->mixednick;
			$FIR->invite(); //邀请逻辑
		}
		
		$this->assign("appname",cookie("appname"));
		$this->assign("codeimg",$codeimg);
		$this->assign("sid",$this->sid);
		$this->assign("user",$this->user);
	}
}